﻿/**********************************************************************
/// <summary>
// Name of File     : LoggingManager.cs
// Author           : Parminder Singh
// Create date      : 17th Sep 2011
/// </summary>
************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.IO;

namespace Crystal.CLL
{
    public static class TitleManager
    {
        #region Member Variable
        private static Dictionary<string, string> _currentTitle = null;

        #endregion

        #region Constructors
        static TitleManager()
        {
            LoadTitleFromXml();
        }
        #endregion

        #region Methods
        /// <summary>
        /// This Function is used to Reload Message From XML File it Check that Hashtable object Current is null
        /// then it Reload message from Xml Files
        /// </summary>
        public static void ReLoadTitleFromXml()
        {
            _currentTitle = null;

            LoadTitleFromXml();
        }

        /// <summary>
        /// This Function is used to load Message From XML File it Check that Hashtable object Current is null
        /// then it Load message from Xml Files Otherwise it it doesnot Load message
        /// </summary>
        private static void LoadTitleFromXml()
        {
            if (_currentTitle == null)
            {
                DataSet dsTitle = new DataSet();

                Assembly _assembly;
                _assembly = Assembly.GetExecutingAssembly();
                dsTitle.ReadXml(_assembly.GetManifestResourceStream("Crystal.CLL.Components.EntityFiles.PageTitle.xml"));
                _currentTitle = new Dictionary<string, string>();

                if (dsTitle.Tables.Count != 0)
                {
                    DataTable dtTitle = dsTitle.Tables["Page"];
                    if (dtTitle.Rows.Count != 0)
                    {
                        foreach (DataRow drTitle in dtTitle.Rows)
                        {
                            _currentTitle.Add(Convert.ToString(drTitle["url"]), Convert.ToString(drTitle["title"]));
                        }
                    }
                    dtTitle = null;
                }
                dsTitle = null;
            }
        }

        /// <summary>
        /// Get The Message from Message Hashtable and format it with the argumaents passed.
        /// </summary>
        /// <param name="strMessageName">string</param>
        /// <param name="args">param object[]</param>       
        /// <returns>string</returns>
        public static string GetTitle(string pageURL)
        {
            return (_currentTitle.ContainsKey(pageURL) == true ? Convert.ToString(_currentTitle[pageURL]) : Convert.ToString(_currentTitle[""]));
        }
        #endregion
    }
}
